package com.indsvr.service;

import com.google.gson.Gson;
import com.indsvr.entity.xunlei.XunLeiFileList;
import com.indsvr.entity.xunlei.XunLeiResponse;
import com.indsvr.entity.xunlei.XunLeiPostTask;
import com.indsvr.utils.XunleiOkHttpUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class TaskService {

    public void startTask(XunleiOkHttpUtils xunleiOkHttpUtils, String id, String deviceId) throws Exception {
        updateTask(xunleiOkHttpUtils,id,deviceId,"{\"phase\":\"running\"}");
    }
    public void stopTask(XunleiOkHttpUtils xunleiOkHttpUtils, String id, String deviceId) throws Exception {
        updateTask(xunleiOkHttpUtils,id,deviceId,"{\"phase\":\"pause\"}");
    }


    private void updateTask(XunleiOkHttpUtils xunleiOkHttpUtils, String id, String deviceId,String spec) throws Exception {
        String url = "/method/patch/drive/v1/task";

        HashMap<String, Object> body = new HashMap<>();
        body.put("space", deviceId);
        body.put("type", "user#download-url");
        body.put("id", id);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("spec", spec);
        body.put("set_params", objectObjectHashMap);

        xunleiOkHttpUtils.execute(url,null, body);
    }


    public void delTask(XunleiOkHttpUtils xunleiOkHttpUtils, String id) throws Exception {
        String url = "/method/delete/drive/v1/tasks";
        HashMap<String, String> params = new HashMap<>();
        params.put("task_ids", id);
        xunleiOkHttpUtils.execute(url, params, null);
    }

    public XunLeiResponse addTask(XunleiOkHttpUtils xunleiOkHttpUtils, String id, String url,String deviceId) throws Exception {
        XunLeiFileList xunLeiFileList = listFiles(url, xunleiOkHttpUtils);
        if (xunLeiFileList.getList()==null) {
            throw new RuntimeException("下载链接有误，不存在下载文件");
        }
        XunLeiPostTask xunLeiPostTask = new XunLeiPostTask(xunLeiFileList, deviceId, getFileIndex(xunLeiFileList), id,url);

        String taskUrl = "/drive/v1/task";

        String bodyExecute = xunleiOkHttpUtils.execute(taskUrl, null ,xunLeiPostTask);

        return new Gson().fromJson(bodyExecute, XunLeiResponse.class);
    }

    private XunLeiFileList listFiles(String url, XunleiOkHttpUtils xunleiOkHttpUtils) throws  Exception{
        String list_files_path =  "/drive/v1/resource/list";
        HashMap<String, String> formBody = new HashMap<>();
        formBody.put("urls", url);
        String bodyExecute = xunleiOkHttpUtils.execute(list_files_path,null,formBody);
        return new Gson().fromJson(bodyExecute, XunLeiFileList.class);
    }

    private String getFileIndex(XunLeiFileList xunLeiFileList){
        long fileCount = xunLeiFileList.getList().getResources().get(0).getFile_count();
        if (fileCount==1) {
            return "--1,";
        }
        long max_sub_file_idx=0;
        for (XunLeiFileList.DirResource resource : xunLeiFileList.getList().getResources().get(0).getDir().getResources()) {
            long fileIndex = resource.getFile_index();
            if (fileIndex > max_sub_file_idx) {
                max_sub_file_idx = fileIndex;
            }
        }
        return "0-"+max_sub_file_idx;
    }

}
